#ifndef BACKUPENGINE_H
#define BACKUPENGINE_H
#include <QtGui>
#include <QQueue>
#include <QSharedPointer>
#include "fileinfo.h"
#include "backuptask.h"
#include "restoretask.h"
#include "XDeltaWrapper.h"
#include "localtargetadapter.h"

class BackupEngine : public QObject
{
    Q_OBJECT

public:
    BackupEngine();
    ~BackupEngine();
    void toggleSuspend();
    void invokeStop();
private:
    typedef enum {
        OK = 0,
        Canceled = 1,
        WrongDir = 2
    } EngineError;

    QTimer *timer;

    /* Backup methods */
    BackupEngine::EngineError backupFilesOnly(AbstractTargetAdapter &adapter);
    BackupEngine::EngineError doMainBackup(AbstractTargetAdapter &adapter, BackupTask::VerificationType vType);
    BackupEngine::EngineError doDifferentialBackup(AbstractTargetAdapter &adapter, BackupTask::VerificationType vType);

    /* Restoration methods */
    BackupEngine::EngineError restoreFilesOnly(AbstractTargetAdapter &adapter);
    BackupEngine::EngineError doMainRestore(AbstractTargetAdapter &adapter, BackupTask::VerificationType vType);
    BackupEngine::EngineError doIncrementalRestore(AbstractTargetAdapter &adapter, BackupTask::VerificationType vType);

    void checkPendingTasks();

    void trySuspend();
    BackupEngine::EngineError tryStop();

    QQueue<QSharedPointer<BackupTask> > taskQueue;
    QSharedPointer<BackupTask> activeTask;

    QMutex suspendMutex;
    bool shouldSuspend;
    bool shouldStop;
    QWaitCondition suspendCondition;

private slots:
    void checkAvailableTasks();
    void doBackup(QSharedPointer<BackupTask> task);
    void doRestore(QSharedPointer<RestoreTask> task);
public slots:
    void performTask(QSharedPointer<BackupTask> task);

signals:
    void progressMaximumChanged(quint64 maximum);
    void progressValueAdd(quint64 value);
    void progressMessageChanged(const QString &message);
    void newMessage(const QString &message);
    void changeCurrentTab(quint8 index);
    void backupFinished();
};

#endif // BACKUPENGINE_H
